<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">

      <el-form-item label="账号" prop="username">
        <el-input
          v-model="queryParams.username"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="微信号" prop="wx">
        <el-input
          v-model="queryParams.wx"
          placeholder="请输入微信号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="openid" prop="openId">
        <el-input
          v-model="queryParams.openId"
          placeholder="请输入openid"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="姓名" prop="nickname">
        <el-input
          v-model="queryParams.nickname"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="身份证号" prop="idcard">
        <el-input
          v-model="queryParams.idcard"
          placeholder="请输入身份证号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="通信地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入通信地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="手机号" prop="mobile">
        <el-input
          v-model="queryParams.mobile"
          placeholder="请输入手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="小区" prop="school">
        <el-select v-model="form.school"  placeholder="请选择小区">
          <el-option v-for="item in districtsList" :key="item.id" :label="item.name" :value="item.id"  ></el-option>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
    <!--
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['course:users:add']"
        >新增</el-button>
      </el-col>

       <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['course:users:remove']"
        >删除</el-button>
      </el-col>
      -->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['course:users:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['course:users:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="usersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户邀请人" align="center" prop="inviter1Name" min-width="150" />
      <el-table-column label="教练邀请人" align="center" prop="inviter2Name" min-width="150" />
      <el-table-column label="账号" align="center" prop="username"  min-width="120"/>
      <el-table-column label="微信号" align="center" prop="wx"  min-width="120" />
      <el-table-column label="openid" align="center" prop="openId"  min-width="180" />
      <el-table-column label="姓名" align="center" prop="nickname"   min-width="120"/>
      <el-table-column label="身份证号" align="center" prop="idcard"   min-width="150"/>
      <el-table-column label="性别" align="center" prop="sex"   min-width="50">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="通信地址" align="center" prop="address" min-width="150" />
      <el-table-column label="出生日期" align="center" prop="birthday" min-width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="mobile" min-width="150"/>
      <el-table-column label="头像" align="center" prop="avatar"  min-width="150"/>
      <el-table-column label="小区" align="center" prop="school" min-width="150" />
      <el-table-column label="备注" align="center" prop="remark"  min-width="150"/>
      <el-table-column label="账号状态" align="center" prop="status"  min-width="100">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_business_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="积分" align="center" prop="point"  min-width="100"/>
      <el-table-column label="纬度" align="center" prop="lat"   min-width="100"/>
      <el-table-column label="经度" align="center" prop="lon"  min-width="100"/>
      <el-table-column label="最后登录时间" align="center" prop="lastLoginTime" width="150">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>

      <el-table-column label="操作" align="center" class-name="small-padding fixed-width"  min-width="180">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['course:users:edit']"
          >修改</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改学员对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户邀请人id" prop="inviter1Id">
          <el-input v-model="form.inviter1Id" placeholder="请输入用户邀请人id" />
        </el-form-item>
        <el-form-item label="教练邀请人id" prop="inviter2Id">
          <el-input v-model="form.inviter2Id" placeholder="请输入教练邀请人id" />
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="form.username" placeholder="请输入账号" />
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="form.password" placeholder="请输入密码" />
        </el-form-item>
        <el-form-item label="微信号" prop="wx">
          <el-input v-model="form.wx" placeholder="请输入微信号" />
        </el-form-item>
        <el-form-item label="openid" prop="openId">
          <el-input v-model="form.openId" placeholder="请输入openid" />
        </el-form-item>
        <el-form-item label="姓名" prop="nickname">
          <el-input v-model="form.nickname" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="身份证号" prop="idcard">
          <el-input v-model="form.idcard" placeholder="请输入身份证号" />
        </el-form-item>
        <el-form-item label="通信地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入通信地址" />
        </el-form-item>
        <el-form-item label="出生日期" prop="birthday">
          <el-date-picker clearable
            v-model="form.birthday"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择出生日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="form.mobile" placeholder="请输入手机号" />
        </el-form-item>
        <el-form-item label="头像" prop="avatar">
          <el-input v-model="form.avatar" placeholder="请输入头像" />
        </el-form-item>
        <el-form-item label="小区" prop="school">
          <el-select v-model="form.school"  placeholder="请选择小区">
           <el-option v-for="item in districtsList" :key="item.id" :label="item.name" :value="item.id"  ></el-option>
         </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <el-form-item label="积分" prop="point">
          <el-input v-model="form.point" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="纬度" prop="lat">
          <el-input v-model="form.lat" placeholder="请输入纬度" />
        </el-form-item>
        <el-form-item label="经度" prop="lon">
          <el-input v-model="form.lon" placeholder="请输入经度" />
        </el-form-item>
        <el-form-item label="最后登录时间" prop="lastLoginTime">
          <el-date-picker clearable
            v-model="form.lastLoginTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择最后登录时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUsers, getUsers, delUsers, addUsers, updateUsers } from "@/api/course/users";
import { allDistricts} from "@/api/course/districts";

export default {
  name: "Users",
  dicts: ['sys_user_sex','sys_business_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员表格数据
      usersList: [],
      districtsList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        inviter1Id: null,
        inviter2Id: null,
        username: null,
        password: null,
        wx: null,
        openId: null,
        nickname: null,
        idcard: null,
        sex: null,
        address: null,
        birthday: null,
        mobile: null,
        avatar: null,
        school: null,
        status: null,
        point: null,
        lat: null,
        lon: null,
        lastLoginTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        inviter1Id: [
          { required: true, message: "用户邀请人id不能为空", trigger: "blur" }
        ],
        inviter2Id: [
          { required: true, message: "教练邀请人id不能为空", trigger: "blur" }
        ],
        username: [
          { required: true, message: "账号不能为空", trigger: "blur" }
        ],
        password: [
          { required: true, message: "密码不能为空", trigger: "blur" }
        ],
        wx: [
          { required: true, message: "微信号不能为空", trigger: "blur" }
        ],
        openId: [
          { required: true, message: "openid不能为空", trigger: "blur" }
        ],
        nickname: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
        idcard: [
          { required: true, message: "身份证号不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "性别：1男，2女不能为空", trigger: "change" }
        ],
        address: [
          { required: true, message: "通信地址不能为空", trigger: "blur" }
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" }
        ],
        avatar: [
          { required: true, message: "头像不能为空", trigger: "blur" }
        ],
        school: [
          { required: true, message: "小区不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "账号状态：1正常，2禁用不能为空", trigger: "change" }
        ],
        point: [
          { required: true, message: "积分不能为空", trigger: "blur" }
        ],
        lat: [
          { required: true, message: "纬度不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getAllDistricts();
  },
  methods: {
   /** 查询小区列表 */
   getAllDistricts() {
      allDistricts().then(response => {
        this.districtsList = response.data;
      });
    },
    /** 查询学员列表 */
    getList() {
      this.loading = true;
      listUsers(this.queryParams).then(response => {
        this.usersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        inviter1Id: null,
        inviter2Id: null,
        username: null,
        password: null,
        wx: null,
        openId: null,
        nickname: null,
        idcard: null,
        sex: null,
        address: null,
        birthday: null,
        mobile: null,
        avatar: null,
        school: null,
        remark: null,
        status: null,
        point: null,
        lat: null,
        lon: null,
        lastLoginTime: null,
        createTime: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUsers(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学员";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateUsers(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUsers(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员编号为"' + ids + '"的数据项？').then(function() {
        return delUsers(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/users/export', {
        ...this.queryParams
      }, `users_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
